package com.cskaoyan.controller;

import com.cskaoyan.aspect.ValidateParameter;
import com.cskaoyan.bean.BO.AdminCouponBO;
import com.cskaoyan.bean.PO.MarketCoupon;
import com.cskaoyan.bean.PO.MarketCouponUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 优惠券管理模块
 * @author wenkaikai
 * @since 2022/08/13 17:11
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController {


    @Autowired
    AdminCouponService adminCouponService;

    /**
     * 好家伙，goodsValue 这里是Integer[]  ,下面又变string了
     * 前端这样搞的吗?
     * @param adminCouponBO
     * @return
     */

    @RequiresPermissions("admin:coupon:create")
    @RequestMapping("create")
    @ValidateParameter
    public BaseRespVo create(@RequestBody @Valid AdminCouponBO adminCouponBO, BindingResult bindingResult) {
        // if (adminCouponBO.getMin().intValue() <= 0) {
        //     return BaseRespVo.custom(400, null, "最低消费必须大于0！");
        // }
        // if (adminCouponBO.getDiscount().intValue() > adminCouponBO.getMin().intValue()) {
        //     return BaseRespVo.custom(400, null, "满减金额不能大于最低消费金额！");
        // }
        // if (adminCouponBO.getLimit().intValue() < 0) {
        //     return BaseRespVo.custom(400, null, "限领数量不能为负数！");
        // }
        // if (adminCouponBO.getTotal().intValue() < 0) {
        //     return BaseRespVo.custom(400, null, "优惠券数量不能为负数！");
        // }
        if (adminCouponBO.getTimeType().intValue() == 0) {
            if (adminCouponBO.getDays().intValue() <= 0) {
                return BaseRespVo.custom(400, null, "有效期天数需大于0！");
            }
        } else {
            if (adminCouponBO.getStartTime().getTime() > adminCouponBO.getEndTime().getTime()) {
                return BaseRespVo.custom(400, null, "有效期截止时间需大于生效时间！");
            }
        }
        AdminCouponBO coupon = adminCouponService.insertCoupon(adminCouponBO);
        return BaseRespVo.ok(0, coupon, "成功");
    }

    @RequiresPermissions("admin:coupon:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon) {
        if (marketCoupon.getDeleted()) {
            // 如果已经是被删除了的券就不能再删除
            return BaseRespVo.ok(400, null, "该券已被删除，不能重复删除！");
        }
        int code = adminCouponService.deleteCoupon(marketCoupon);
        if (code == 1) {
            return BaseRespVo.ok(0, null, "成功");
        }
        return BaseRespVo.ok(400, null, "删除失败");
    }

    /**
     * 优惠券列表
     * @return
     */
    @RequiresPermissions("admin:coupon:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, String name, Short type, Short status) {
        CommonData<MarketCoupon> data = adminCouponService.queryCouponList(info, name, type, status);
        return BaseRespVo.ok(0, data, "成功");
    }

    /**
     * 领取了优惠券的用户列表
     */
    @RequiresPermissions("admin:coupon:listuser")
    @RequestMapping("listuser")
    public BaseRespVo listuser(BasePageInfo info, Integer couponId, Integer userId, Short status) {
        CommonData<MarketCouponUser> data = adminCouponService.queryCouponUserList(info, couponId, userId, status);
        return BaseRespVo.ok(0, data, "成功");
    }

    /**
     * 回显某个优惠券详情
     * @param id
     * @return
     */
    @RequiresPermissions("admin:coupon:read")
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        if (id == null || "".equals(id)) {
            return BaseRespVo.ok(400, null, "请求参数有误！");
        }
        MarketCoupon coupon = adminCouponService.queryCouponById(id);
        return BaseRespVo.ok(0, coupon, "成功");
    }

    /**
     * 编辑优惠券
     * 返回类型刚好是
     * @param
     * @return
     */
    @RequiresPermissions("admin:coupon:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCoupon marketCoupon) {
        if (marketCoupon.getMin().intValue() <= 0) {
            return BaseRespVo.custom(400, null, "最低消费必须大于0！");
        }
        if (marketCoupon.getDiscount().intValue() > marketCoupon.getMin().intValue()) {
            return BaseRespVo.custom(400, null, "满减金额不能大于最低消费金额！");
        }
        if (marketCoupon.getLimit().intValue() < 0) {
            return BaseRespVo.custom(400, null, "限领数量不能为负数！");
        }
        if (marketCoupon.getTotal().intValue() < 0) {
            return BaseRespVo.custom(400, null, "优惠券数量不能为负数！");
        }
        if (marketCoupon.getTimeType().intValue() == 0) {
            if (marketCoupon.getDays().intValue() <= 0) {
                return BaseRespVo.custom(400, null, "有效期天数需大于0！");
            }
        } else {
            if (marketCoupon.getStartTime().getTime() > marketCoupon.getEndTime().getTime()) {
                return BaseRespVo.custom(400, null, "有效期截止时间需大于生效时间！");
            }
        }
        MarketCoupon updatedCoupon = adminCouponService.updateCoupon(marketCoupon);
        if (updatedCoupon != null) {
            return BaseRespVo.ok(0, updatedCoupon, "成功");
        }
        return BaseRespVo.custom(400, null, "编辑失败");
    }

}